let domainConfig = []
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
const getConfig = async (type) => {
    const response = await fetch('http://localhost:8083/businessservice/domainJobConfig/getConfig?pullType=' + type, {
        method: 'GET',
    })
    const data = await response.json();
    if (type == 12) {
        domainConfig = data.map(res => {
            res.openUrl = String(res.requestBody).replace("formatStartDate", "20241101").replace("formatEndDate", "20241107")
            return res
        })
    } else {
        domainConfig = data
    }
}
const formatDateTime = (strDate) => {
    if (!strDate) return undefined
    let d = new Date(strDate)
    const year = d.getFullYear();
    const month = (d.getMonth() + 1).toString().padStart(2, '0');
    const day = d.getDate().toString().padStart(2, '0');
    const hours = d.getHours().toString().padStart(2, '0');
    const minutes = d.getMinutes().toString().padStart(2, '0');
    const seconds = d.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
async function lookerstudioExec() {
    await pushEle("开始解析数据___________________________________________________________" + domainConfig.length)
    for (let index = 0; index < domainConfig.length; index++) {
        await pushEle(domainConfig[index].domainId)
        chrome.tabs.create({ url: domainConfig[index].openUrl + "?domainId=" + domainConfig[index].domainId }, (tab) => {
            setTimeout(() => chrome.tabs.remove(tab.id), 6000)
        })
        await sleep(7000)
    }
    await pushEle("解析数据完成___________________________________________________________")
}

const pushEle = async (message) => {
    const ele = document.querySelector("#list")
    const child = document.createElement("h1")
    child.innerText = formatDateTime(new Date()) + "   " + message
    ele.appendChild(child)
}
chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
        pushEle(request.greeting)
        if (request.type === "error") {
            console.log(request.greeting)
            return
            const headers = new Headers()
            headers.append('Content-Type', 'application/json');
            fetch("https://open.feishu.cn/open-apis/bot/v2/hook/fc58786f-3834-4e1a-b0b6-fdbb7b55dbf2", {
                method: 'POST',
                headers: headers,
                body: JSON.stringify({
                    "msg_type": "text",
                    "content": {
                        "text": request.greeting
                    }
                })
            })
        }

    }
);


chrome.alarms.onAlarm.addListener(async function (param) {
    switch (param.name) {
        case "lookerstudio":
            await getConfig(5)
            lookerstudioExec()
            break;
        case "sensors":
            await getConfig(12)
            lookerstudioExec()
            break;
        default:
            break;
    }
})